package com.hfzy.ihk.web.aliyuncall.biz;


import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.facade.aliyuncall.enums.MainCallTypeEnum;
import com.hfzy.ihk.web.aliyuncall.constant.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/22.
 */
@SuppressWarnings("all")
@Component
public class CallHistoryLastUtils {

    private static Logger logger = LoggerFactory.getLogger(CallHistoryLastUtils.class);

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 缓存最后使用某主显号码拨打给某客户号码的信息
     * phone:userId 经纪ID
     * phone:dataId 置业的CallHistoryId
     * @param showNum
     * @param mainCallPhone
     * @param targetCallPhone
     * @param createUserId
     * @param dataId
     * @param mainCallType
     */
    public  void addCallHistoryLastByShowNum(String showNum, String mainCallPhone, String targetCallPhone,
                                                   String createUserId, String dataId, String mainCallType,String customerName,
                                                   String pageFrom,String project,String commitTime,String cityId) {

        String customerPhone="";//客户号码

        if (String.valueOf(MainCallTypeEnum.MAIN_CALL_PHONE_IS_CUSTOMER.getValue()).equals(mainCallType)) {

            customerPhone = mainCallPhone;

        } else if (String.valueOf(MainCallTypeEnum.MAIN_CALL_PHONE_IS_SALES.getValue()).equals(mainCallType)) {

            customerPhone = targetCallPhone;

        } else {
            logger.error("setToRedis error,whereFrom is:{}", mainCallType);
            return;
        }

        if(StringUtil.isEmpty(customerPhone)){
            logger.error("客户号码空，setToRedis error,whereFrom is:{}", mainCallType);
            return;
        }

        Map<String, String> map = new HashMap<String, String>();

        map.put(RedisKey.LAST_CALL_INFO_USER_ID, createUserId);
        map.put(RedisKey.LAST_CALL_INFO_DATA_ID, dataId);
        if(StringTools.isNotempty(cityId)&&!cityId.equalsIgnoreCase("null")) {
            map.put(RedisKey.LAST_CALL_INFO_CITY_ID, cityId);
        }else{
            map.put(RedisKey.LAST_CALL_INFO_CITY_ID, "-1");
        }

        Map temp = new HashMap();
        temp.put("customerName",customerName);
        temp.put("mainCallPhone",customerPhone);//客户号码
        temp.put("pageFrom",pageFrom);
        temp.put("project",StringTools.isNotempty(project)?project:"NULL");
        temp.put("commitTime", DateUtils.formatDate(DateUtils.getDateFromString(commitTime,"yyyyMMddHHmmssSSS")));

        map.put(RedisKey.POPUP_WINDOW_ID, JSONObject.toJSONString(temp));
        stringRedisTemplate.opsForHash().putAll(RedisKey.createLastCallInfo(showNum,customerPhone),map);
       // RedisUtils.addHashMuitLimitTime(RedisKey.createLastCallInfo(showNum,customerPhone),map, RedisStringUtils.CACHE_ONE_MONTH);

    }
}
